/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reportcenter.admin.ui;

import org.springframework.richclient.application.event.LifecycleApplicationEvent;
import org.springframework.richclient.dialog.CloseAction;
import org.springframework.richclient.dialog.ConfirmationDialog;
import org.springframework.richclient.dialog.FormBackedDialogPage;
import org.springframework.richclient.dialog.TitledPageApplicationDialog;
import org.springframework.richclient.form.Form;
import org.springframework.util.Assert;
import reportcenter.admin.business.AdminReportDao;
import reportcenter.report.dominio.report.Report;

/**
 *
 * @author renan_luz
 */
public class AdminReportPropertiesDialog extends TitledPageApplicationDialog {

    private Form form;
    private boolean creatingNew = false;
    private AdminReportDao dao;

    public AdminReportPropertiesDialog(AdminReportDao dao) {
        this(null, dao);
    }

    public AdminReportPropertiesDialog(Report report, AdminReportDao dao) {
        Assert.notNull(dao, "Dao necessário para editar o relatório");
        if (report == null) {
            creatingNew = true;
            report = new Report();
        }
        setCloseAction(CloseAction.DISPOSE);
        form = new AdminReportForm(report);
        setDialogPage(new FormBackedDialogPage(form));
        this.dao = dao;
    }

    private Report getEditingReport() {
        return (Report) form.getFormModel().getFormObject();
    }

    protected void onAboutToShow() {
        if (creatingNew) {
            getMessage("adminReportProperties.new.title");
            setTitle(getMessage("adminReportProperties.new.title"));
        } else {
            Report report = getEditingReport();
            String title = getMessage("adminReportProperties.edit.title", new Object[]{report.getTitle()});
            setTitle(title);
        }
    }

    protected boolean onFinish() {
        // commit any buffered edits to the model
        form.getFormModel().commit();
        // Update the persistent store with the new/modified object.
        String eventType;
        if (creatingNew) {
            eventType = LifecycleApplicationEvent.CREATED;
            dao.add(getEditingReport());
        } else {
            eventType = LifecycleApplicationEvent.MODIFIED;
        }
        // And notify the rest of the application of the change
        getApplicationContext().publishEvent(new LifecycleApplicationEvent(eventType, getEditingReport()));
        return true;
    }

    protected void onCancel() {
        // Warn the user if they are about to discard their changes
        if (form.getFormModel().isDirty()) {
            String msg = getMessage("adminReportProperties.dirtyCancelMessage");
            String title = getMessage("adminReportProperties.dirtyCancelTitle");
            ConfirmationDialog dlg = new ConfirmationDialog(title, msg) {

                protected void onConfirm() {
                    AdminReportPropertiesDialog.super.onCancel();
                }
            };
            dlg.showDialog();
        } else {
            super.onCancel();
        }
    }
}
